'use strict';
app.service('roleService',['$http','$q','utilService',function($http,$q,utilService){

    this.createRole = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'role',
            data:$scope.role
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.findById = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'role/'+ $scope.role.id,
            params:{id:$scope.role.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.updateRole = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'role/'+ $scope.role.id,
            data:$scope.role,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.getCountByRoleId = function($scope,id){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'role/used/count',
            params:{id:id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.delete = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'role/'+ $scope.role.id,
            data:{id:$scope.role.id},
            method:'DELETE'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
}]);